<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #result {
            width: 200px;
            height: 200px;
            border: 2px solid pink;
        }
    </style>
</head>
<body>
<h1>原生Ajax</h1>
    <button>点击发送请求</button>
    <div id="result"></div>
<script>
    const btn = document.getElementsByTagName("button")[0];
    var res = document.getElementById("result");
    btn.onclick = function () {
        //创建对象
        const xhr = new XMLHttpRequest();
        //初始化 设置请求方法和url
        xhr.open('GET', 'http://localhost:8080/test1');
        //发送
        xhr.send();
        //事件绑定
        //readyState是xhr对象中的属性，标识状态0,1,2,3,4
        xhr.onreadystatechange = function () {
            //判断 服务器返回的所有结果
            if (xhr.readyState === 4) {
                //判断响应状态码
                if (xhr.status >= 200 && xhr.status < 300) {
                    res.innerHTML = xhr.response;
                }
            }
        }
    }
</script>
</body>
</html>